package com.cheyun.netsalev3.viewmodel

import android.app.Activity
import android.content.Intent
import android.view.View
import com.cheyun.netsalev3.repository.LoginRepository
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.utils.*
import com.cheyun.netsalev3.widget.PopDialog
import android.os.CountDownTimer
import android.widget.DatePicker
import com.cheyun.netsalev3.view.UserFaceActivity
import com.cheyun.netsalev3.widget.DatePickerFragment
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlin.math.log

class UserSettingActivityViewModel : BaseViewModel() {

    var birthday = ObservableField<String>("")
    var realname = ""
    var sex = "0"
    var email = ""
    var staffnum = ""
    var idnumber = ""
    var headimg = MutableLiveData<String>()

    private var loginRepository = LoginRepository()

    var activity: Activity? = null

    init {
        var loginData = MySharedPreferences.getLoginData()
        loginData?.apply {
            realname = this.member.realname
            sex = this.member.sex.toString()
            birthday.set(this.member.birthday)
            email = this.member.email
            staffnum = this.member.staffnum
            idnumber = this.member.idnumber
            headimg.postValue(this.member.headimg)
        }
    }

    fun onClickFace(view:View){
        var content = view.context
        if (content is AppCompatActivity) {
            var intent = Intent(view.context, UserFaceActivity::class.java)
            view.context.startActivity(intent)
        }
    }

    fun onClickBirthday(view:View){
        var content = view.context
        if (content is AppCompatActivity) {
            val datePickerFragment = DatePickerFragment()
            datePickerFragment.clickOk = this::onDateSetstart
            datePickerFragment.show(content.supportFragmentManager, "datePicker")
        }
    }
    fun onDateSetstart(view: DatePicker, year: Int, month: Int, day: Int) {
        birthday.set("$year-$month-$day")
    }

    fun setUserSex(i: Int) {
        sex = i.toString()
    }
    fun setFace(f: String) {
        headimg.postValue(f)
    }
    fun onSubmit(view:View){
        var method: () -> Unit = {
            MyToast("提交成功")
            var loginData = MySharedPreferences.getLoginData()
            loginData?.member?.birthday = birthday.get()!!
            loginData?.member?.realname = realname
            loginData?.member?.headimg = headimg.value!!
            loginData?.member?.sex = sex.toInt()
            loginData?.member?.email = email
            loginData?.member?.idnumber = idnumber
            loginData?.member?.staffnum = staffnum
            MySharedPreferences.putLoginData(loginData!!)
            LiveEventBus.get().with("userinfoupdate").postValue("")
            myFinish()
        }
        loginRepository.editProfile(sex,realname,birthday.get()!!,headimg.value!!,staffnum,email,idnumber,method)
    }

    var totalTime = 60000L
    var oneceTime = 1000L
    var showTime = MutableLiveData<String>()
    var currentTime = 0L
    private val countDownTimer = object : CountDownTimer(totalTime, oneceTime) {
        override fun onTick(millisUntilFinished: Long) {
            val value = (millisUntilFinished / 1000).toInt().toString()
            currentTime = millisUntilFinished/1000
            showTime.postValue(value)
        }

        override fun onFinish() {
            currentTime = 0
            showTime.postValue("0")
        }
    }

}